#include <QtDebug>

#include "listgroup.h"
#include "ui_listgroup.h"

#include "connection.h"

ListGroup::ListGroup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ListGroup)
{
    ui->setupUi(this);

    tblGroups = new QSqlTableModel(this);
    tblGroups->setTable("\"V_GROUP\"");
    tblGroups->select();

    ui->tableGroups->setModel(tblGroups);

    setNameFields();

    modelTreeGroup.clear();
    //loadTreeGroup(modelTreeGroup.invisibleRootItem(), 0);

    QStandardItem *item = new QStandardItem(tr("All group"));
    modelTreeGroup.invisibleRootItem()->appendRow(item);
    loadTreeGroup(item, 0);
    ui->treeGroup->setModel(&modelTreeGroup);
}

ListGroup::~ListGroup()
{
    delete ui;
}

void ListGroup::setNameFields()
{

    tblGroups->setHeaderData(V_GROUP_group_name, Qt::Horizontal, tr("Group"));
    //tblGroups->setHeaderData(ViewGroup_small, Qt::Horizontal, tr("Group small"));

    ui->tableGroups->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableGroups->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableGroups->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tableGroups->setColumnHidden(V_GROUP_group_id, true);
    ui->tableGroups->setColumnHidden(V_GROUP_curriculum_id, true);
    ui->tableGroups->setColumnHidden(V_GROUP_group_stream, true);
    ui->tableGroups->setColumnHidden(V_GROUP_group_year_begin, true);
    ui->tableGroups->setColumnHidden(V_GROUP_curriculum_base, true);
    ui->tableGroups->setColumnHidden(V_GROUP_curriculum_form, true);
    ui->tableGroups->setColumnHidden(V_GROUP_curriculum_budget, true);
    ui->tableGroups->setColumnHidden(V_GROUP_speciality_id, true);

    ui->tableGroups->resizeColumnsToContents();

    QHeaderView *header = ui->tableGroups->horizontalHeader();
    header->setStretchLastSection(true);
}

void ListGroup::loadTreeGroup(QStandardItem *parentItem, int pid)
{

    QSqlQuery query;
    query.exec("SELECT \"TREEGROUP_ID\", \"TREEGROUP_PID\", \"treename\" FROM \"V_TREEGROUP\" WHERE \"TREEGROUP_PID\" = "+QString::number(pid));
    qDebug() << query.lastQuery();

    while (query.next())
    {
        QStandardItem *item = new QStandardItem(query.value(2).toString());
        parentItem->appendRow(item);

        loadTreeGroup(item, query.value(0).toInt());

        //districts.prepare("SELECT id_tree, pid_tree, name_tree FROM grp_tree where :pid_tree = ");
        //districts.bindValue(":pid_tree", qryTreeGroup.value(0).toString())

    }

}

void ListGroup::on_tableGroups_clicked(const QModelIndex &index)
{
    if (index.isValid())
    {
     //   QSqlRecord rec = tblGroups->record(index.row());
     //   QMessageBox::critical(0, qApp->tr("123"),
     //                         rec.value("grp").toString(), QMessageBox::Cancel);
    }
}


void ListGroup::on_treeGroup_clicked(const QModelIndex &index)
{
    if (index.isValid())
    {

        QSqlRecord rec = tblGroups->record(index.row());
        QMessageBox::critical(0, tr("test message "),
                              rec.value("grp").toString(), QMessageBox::Cancel);
    }
}
